package com.example.pet.dao;

import com.hj.pet.model.PetInfo;
import com.hj.pet.model.QueryCondition;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PetShopDao {
    @SelectKey(keyColumn = "pet_id", keyProperty = "pet_id", before = false, resultType = Integer.class, statement = {" select last_insert_id()"})
    @Insert("insert into pet_info(pet_id,user_account,price,pet_name,age,pet_type,photo_url,gender,phone,detail,weight,modify_time) " +
            "values(#{pet_id},#{user_account},#{price},#{pet_name},#{age},#{pet_type},#{photo_url},#{gender},#{phone},#{detail},#{weight},now())")
    int save (PetInfo petInfo);

    @Update("<script>"+
            "update pet_info" +
            "        <set>" +
            "                price=#{price},age=#{age},gender=#{gender}," +
            "            <if test=\"null !=pet_name\">" +
            "                pet_name=#{pet_name}," +
            "            </if>" +
            "            <if test=\"null !=pet_type\">" +
            "                pet_type=#{pet_type}," +
            "            </if>" +
            "            <if test=\"null !=phone\">" +
            "                phone=#{phone}," +
            "            </if>" +
            "            <if test=\"null !=weight\">" +
            "                weight=#{weight}," +
            "            </if>" +
            "            <if test=\"null !=detail\">" +
            "                detail=#{detail}," +
            "            </if>" +
            "            <if test=\"null !=photo_url\">" +
            "                photo_url=#{photo_url}" +
            "            </if>" +
            "            <if test=\"null !=modify_time\">" +
            "                modify_time=now()" +
            "            </if>" +
            "        </set>" +
            "        where pet_id=#{pet_id}"
            +"</script>")
    int updateById (PetInfo petInfo);

    @Select("select * from pet_info where pet_id=#{petId}")
    PetInfo findPetById(int petId);

    @Select("select * from pet_info where pet_type=#{type}")
    List<PetInfo> findPetsByType(String type);

    @Select("select * from pet_info where price>#{price1} && price<#{price2}")
    List<PetInfo> findPetsByPrice(int price1, int price2);

    @Select("select * from pet_info where user_id=#{user_account}")
    List<PetInfo> findPetsByUserAccount(String user_account);

    @Select("select * from pet_info where pet_name=#{pet_name}")
    List<PetInfo> findPetsByPetName(String pet_name);

    @Select("select * from pet_info")
    List<PetInfo> findAllPets();

    @Delete("delete from pet_info where pet_id=#{petId}")
    int delPetById(int petId);

    // TODO 多条件查询
    @Select("<script>" +
            "select * from pet_info" +
            "        where 1=1" +
            "            <if test=\"user_account != null\">" +
            "                and user_account=#{user_account}" +
            "            </if>" +
            "            <if test=\"start_price !=null \">" +
            "                and price &gt;= #{start_price}" +
            "            </if>" +
            "            <if test=\"end_price !=null \">" +
            "                and price &lt;=#{end_price}" +
            "            </if>" +
            "            <if test='null !=pet_name'>" +
            "                and pet_name like concat('%',#{pet_name},'%')" +
            "            </if>" +
            "            <if test=\"null !=pet_type\">" +
            "                and pet_type like concat ('%',#{pet_type},'%')" +
            "            </if>" +
            "            <if test=\"null !=gender\">" +
            "                and gender=#{gender}" +
            "            </if>" +
            "            <if test=\"null !=weight\">" +
            "                and weight like concat ('%',#{weight},'%')" +
            "            </if>" +
            "</script>")
    List<PetInfo> findPetByQuery(QueryCondition condition);

    @Select("select count(*) from user_info where user_account = #{account}")
    int searchByAccount(String account);

}
